console.log("auth 服务");
import bodyParser from 'body-parser';
import cors from 'cors';
import express from 'express';
import mysql from 'mysql'
// @ts-ignore
import Crypt from 'node-jsencrypt';
import { PrivateKey } from '../common';
import dayjs from 'dayjs';
import { createHash } from 'crypto';
import { v4 as uuidv4 } from 'uuid';

const cache = new Map();

const connection = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: '123456',
    database: 'momodb'
});

connection.connect();


const crypt = new Crypt();
crypt.setKey(PrivateKey);

const app = express()
app.use(cors())
app.use(bodyParser.json())
app.post('/register', function (req, res) {
    console.log("req", req.body);
    let { account, password } = req.body;
    account = crypt.decrypt(account);
    password = crypt.decrypt(password);
    console.log("account", account);
    console.log("password", password);

    const hash = createHash("md5");
    hash.update(password);
    const passwordHash = hash.digest("hex");

    console.log("passwordHash", passwordHash);

    connection.query('insert into user (account, password, created_time) VALUES(?,?,?)',
        [account, passwordHash, dayjs().format('YYYY-MM-DD HH:mm:ss')],
        function (error, results, fields) {
            if (error) {
                console.log(error);
                return;
            }
            console.log(results)
            // connected!
        });
})

app.post('/login', function (req, res) {
    console.log("req", req.body);
    let { account, password } = req.body;
    account = crypt.decrypt(account);
    password = crypt.decrypt(password);
    console.log("account", account);
    console.log("password", password);

    const hash = createHash("md5");
    hash.update(password);
    const passwordHash = hash.digest("hex");

    connection.query('select * from user where account = ? and password = ?',
        [account, passwordHash],
        function (error, results, fields) {
            if (error) {
                console.log(error);
                return;
            }
            console.log(results)
            const token = uuidv4();
            cache.set(token, account);
            console.log("cache", cache);
            res.json({ token });

            // connected!
        });
})

app.listen(3000)